package stack;

import java.util.ArrayDeque;
import java.util.Deque;

/**
 * P71 简化路径
 * */
public class P71 {
    public static void main(String[] args) {
        P71Test01 p71Test01=new P71Test01();

        String s=p71Test01.simplifyPath("/../");

        System.out.println(s);
    }
}

/**
 * 利用栈来返回父级目录
 * */
class P71Test01 {
        Deque<String> stack;
        public String simplifyPath(String path) {
            stack=new ArrayDeque<>();
            String[] s=path.split("/");
            for (String s1:s){
                if(s1.equals("..")){
                    stack.poll();
                    continue;
                }
                if(s1.isEmpty() ||s1.equals("."))
                    continue;

                stack.push(s1);
            }
            /***
             * 源代码比较rang'yu
             */
            String s2="";
            for(String d:stack){
                s2="/"+d+s2;
            }
            if(s2.isEmpty())
                return "/";

//            String s2="/";
//            while(!stack.isEmpty()){
//                s2+=stack.pollLast();
//                s2+="/";
//            }
//            int length=s2.length()-1;
//            if(s2.charAt(length)=='/'){
//                s2=s2.substring(0,length);
//            }
//            if(s2.isEmpty()){
//                s2+="/";
//            }
            return s2;
        }
}